// Copyright (c) 2012 NetPipe
// All Rights Reserved.

/**
 * @file AnalyseBase.h
 * 分析基础类
 * @author fanpc
 * @date 2012/07/25
 * @version 1.0
 * @note Create
 * Date		     Ver	     Name	   Description
 * 20120725		 1.1.0		 fanpc	   create
 */
#ifndef NP_ANALYSER_ANALYSEBASE_H
#define NP_ANALYSER_ANALYSEBASE_H

#include <boost/regex.hpp>
#include <config/Rule.h>
#include <utils/XString.h>
#include <config/Config.h>
#define  SIZE_CONTENT_TMP 1024*1024*10
class AnalyseBase
{
protected:
  AnalyseBase() {
    m_contentTmp = new char[SIZE_CONTENT_TMP];
    memset(m_contentTmp,0x00,SIZE_CONTENT_TMP);
  }
    virtual ~AnalyseBase()
  {
    if(m_contentTmp!=NULL)
    {
      delete[] m_contentTmp;
      m_contentTmp = NULL;
    }
  }

public:
    inline void setContent(const std::string& content)
    {
      if(Config::getInstance()->getConfig()->charset == "gbk")
      {
        XString::gbk2utf8(m_contentTmp,content.c_str(),SIZE_CONTENT_TMP);
      }
      m_content.assign(m_contentTmp);
    }

    inline void setRule(const Rule& rule) { m_rule = rule; m_rule.setNoToBeDesstroy(); }

    inline Rule& getRule(){return m_rule;}

    inline void setArticle(const articleInfo::ArticleInfo& article){m_article = article;}

    inline articleInfo::ArticleInfo& getArticle(){return m_article;}

protected:
    std::string m_content;                            /*<小说信息页/目录页/章节页HTML内容>*/
    Rule m_rule;                                            /**< 规则*/
    articleInfo::ArticleInfo m_article;
    char*   m_contentTmp;
protected:

    /**
      * 用以获取单个匹配信息
      *@param regexName 规则名词
      *@param output 匹配结果
      */
    void matchSingleValue(const std::string& regexName,std::string& content)
    {
      content.clear();
      typedef std::string::const_iterator StringIterator;
      ruleconfiguration::RegexInfo* reg = m_rule.getRegex(regexName);
      StringIterator itr_begin = m_content.begin();
      StringIterator itr_end = m_content.end();

      boost::smatch matchs;
      boost::regex regexExpress(reg->Pattern);
      if(boost::regex_search(itr_begin, itr_end, matchs, regexExpress, reg->Options))
      {
        content = matchs[1];
      }
      if(reg->FilterPattern.empty() == false)
      {
        XString::replaceAll(content, reg->FilterPattern, "");
      }
    }

    /**
      * 将规则里面某个节点匹配出来的内容pattern，替换为过滤字符串FilterPattern
      */
    void replaceMatchValue(const std::string& regexName, std::string& content)
    {
      typedef std::string::const_iterator StringIterator;
      ruleconfiguration::RegexInfo* reg = m_rule.getRegex(regexName);
      StringIterator itr_begin = m_content.begin();
      StringIterator itr_end = m_content.end();

      boost::smatch matchs;
      boost::regex regexExpress(reg->Pattern);
      static string replaceStr;
      if(boost::regex_search(itr_begin, itr_end, matchs, regexExpress, reg->Options))
      {
        replaceStr = matchs[1];
      }
      if(reg->FilterPattern.empty() == false)
      {
        XString::replaceAll(content, replaceStr, reg->FilterPattern);
      }
    }

//    void matchSingleValue(const std::string &regexName, char* output)
//    {
//      typedef std::string::const_iterator StringIterator;
//      ruleconfiguration::RegexInfo* reg = m_rule.getRegex(regexName);
//      StringIterator itr_begin = m_content.begin();
//      StringIterator itr_end = m_content.end();

//      boost::smatch matchs;
//      boost::regex regexExpress(reg->Pattern);
//      if(boost::regex_search(itr_begin, itr_end, matchs, regexExpress, reg->Options))
//      {
//        memcpy(output,matchs[1],strlen(matchs[1]);
//        //output = matchs[1];
//      }
//    }
};

#endif // NP_ANALYSER_ANALYSEBASE_H
